package com.easybuy.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easybuy.sys.model.SysUser;
import com.easybuy.sys.mapper.SysUserMapper;
import com.easybuy.sys.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author SR.Y
 * @since 2020-12-05
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    @Override
    public boolean creat(SysUser user) {
        return save(user);
    }


    @Override
    public boolean update(SysUser user) {
        return updateById(user);
    }

    @Override
    public SysUser login(SysUser user) {
        String uname = user.getUname();
        String pwd = user.getPwd();
        QueryWrapper<SysUser> wrapper = new QueryWrapper<SysUser>();
        LambdaQueryWrapper<SysUser> lambda = wrapper.lambda();
        if (StrUtil.isNotEmpty(uname)) {
            lambda.eq(SysUser::getUname, uname);
        }
        if (StrUtil.isNotEmpty(pwd)) {
            lambda.eq(SysUser::getPwd, pwd);
        }
        return getOne(wrapper);
    }

    @Override
    public Page<SysUser> list(Integer pageSize, Integer pageNum) {
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        return page(page);
    }

    @Override
    public Page<SysUser> list(SysUser user, Integer pageSize, Integer pageNum) {
        String uname = user.getUname();
        Integer level = user.getLevel();
        String phone = user.getPhone();
        Integer state = user.getState();
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysUser> wrapper = new QueryWrapper<SysUser>();
        LambdaQueryWrapper<SysUser> lambda = wrapper.lambda();
        if (StrUtil.isNotEmpty(uname)) {
            lambda.like(SysUser::getUname, uname);
        }
        if (StrUtil.isNotEmpty(phone)) {
            lambda.eq(SysUser::getPhone, phone);
        }
        if(level!=null){
            lambda.eq(SysUser::getLevel,level);
        }
        if(state!=null){
            lambda.eq(SysUser::getState,state);
        }


        return page(page, wrapper);
    }

    @Override
    public SysUser getOneById(Integer id) {
        return getById(id);
    }

    /**
     * 删除
     *
     * @param idList
     * @return
     */
    @Override
    public boolean delete(List<Integer> idList) {
        return removeByIds(idList);
    }


}
